package iam

import (
	"time"
)

// Policy 策略表模型
type Policy struct {
	ID          uint64    `gorm:"primary_key;auto_increment" json:"id"`
	Name        string    `gorm:"type:varchar(100);uniqueIndex;not null;comment:策略名称" json:"name"`
	Description string    `gorm:"type:varchar(500);comment:策略描述" json:"description"`
	Document    string    `gorm:"type:longtext;not null;comment:策略文档JSON" json:"document"`
	Version     string    `gorm:"type:varchar(20);default:'2012-10-17';comment:策略版本" json:"version"`
	IsBuiltin   bool      `gorm:"default:false;comment:是否为内置策略" json:"is_builtin"`
	Status      int       `gorm:"type:tinyint;default:1;comment:状态 1:启用 2:禁用" json:"status"`
	CreateBy    uint64    `gorm:"comment:创建人ID" json:"create_by"`
	UpdateBy    uint64    `gorm:"comment:更新人ID" json:"update_by"`
	CreateTime  time.Time `gorm:"autoCreateTime;comment:创建时间" json:"create_time"`
	UpdateTime  time.Time `gorm:"autoUpdateTime;comment:更新时间" json:"update_time"`
}

// TableName 指定表名
func (Policy) TableName() string {
	return "policies"
}

// RolePolicy 角色策略关系表
type RolePolicy struct {
	ID         uint64    `gorm:"primary_key;auto_increment" json:"id"`
	RoleID     uint64    `gorm:"not null;comment:角色ID" json:"role_id"`
	PolicyID   uint64    `gorm:"not null;comment:策略ID" json:"policy_id"`
	CreateBy   uint64    `gorm:"comment:创建人ID" json:"create_by"`
	CreateTime time.Time `gorm:"autoCreateTime;comment:创建时间" json:"create_time"`
}

// TableName 指定表名
func (RolePolicy) TableName() string {
	return "role_policies"
}

// PolicyStatement 策略语句结构（用于JSON解析）
type PolicyStatement struct {
	Sid       string                 `json:"Sid,omitempty"`       // 语句ID
	Effect    string                 `json:"Effect"`              // Allow 或 Deny
	Action    interface{}            `json:"Action"`              // 动作，可以是字符串或数组
	Resource  interface{}            `json:"Resource,omitempty"`  // 资源，可以是字符串或数组
	Condition map[string]interface{} `json:"Condition,omitempty"` // 条件
}

// PolicyDocument 策略文档结构（用于JSON解析）
type PolicyDocument struct {
	Version   string            `json:"Version"`             // 策略语言版本
	Statement []PolicyStatement `json:"Statement"`           // 策略语句数组
}

// 策略状态常量
const (
	PolicyStatusEnabled  = 1 // 启用
	PolicyStatusDisabled = 2 // 禁用
)

// 策略效果常量
const (
	EffectAllow = "Allow"
	EffectDeny  = "Deny"
)

// 预定义动作常量
const (
	// 用户管理
	ActionUserList   = "user:List"
	ActionUserGet    = "user:Get"
	ActionUserCreate = "user:Create"
	ActionUserUpdate = "user:Update"
	ActionUserDelete = "user:Delete"

	// 用户组管理
	ActionGroupList   = "group:List"
	ActionGroupGet    = "group:Get"
	ActionGroupCreate = "group:Create"
	ActionGroupUpdate = "group:Update"
	ActionGroupDelete = "group:Delete"

	// 角色管理
	ActionRoleList   = "role:List"
	ActionRoleGet    = "role:Get"
	ActionRoleCreate = "role:Create"
	ActionRoleUpdate = "role:Update"
	ActionRoleDelete = "role:Delete"

	// 策略管理
	ActionPolicyList   = "policy:List"
	ActionPolicyGet    = "policy:Get"
	ActionPolicyCreate = "policy:Create"
	ActionPolicyUpdate = "policy:Update"
	ActionPolicyDelete = "policy:Delete"

	// 抖音模块
	ActionDouyinProfileList   = "douyin:profile:List"
	ActionDouyinProfileCreate = "douyin:profile:Create"
	ActionDouyinProfileUpdate = "douyin:profile:Update"
	ActionDouyinProfileDelete = "douyin:profile:Delete"
	ActionDouyinVideoCrawl    = "douyin:video:Crawl"
	ActionDouyinVideoDownload = "douyin:video:Download"

	// 系统管理
	ActionSystemManage = "system:*"
)